package org.sws.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.sws.domain.strategy.enums.LogicModel;
import org.sws.domain.strategy.model.valobj.ChainStrategyAwardVO;
import org.sws.domain.strategy.repository.IStrategyRepository;
import org.sws.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.sws.types.common.Constants;

import javax.annotation.Resource;

/**
 * @Author sws
 * @Date 2025/2/24 16:10
 * @description: 黑名单责任链节点
 */
@Slf4j
@Service("rule_blacklist")
public class BlackListLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository repository;
    @Override
    public ChainStrategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-黑名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        // 基于黑名单的rule_model的值获取对应的ruleValue
        String ruleValue = repository.queryStrategyRuleValue(strategyId, ruleModel());
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        int awardId = Integer.parseInt(splitRuleValue[0]);

        // 获取黑名单用户列表
        String[] userBlackIds = splitRuleValue[1].split(Constants.SPLIT);
        for (String userBlackId : userBlackIds) {
            if (userId.equals(userBlackId)) {
                log.info("抽奖责任链-黑名单接管 userId: {} strategyId: {} ruleModel: {} awardId: {}", userId, strategyId, ruleModel(), awardId);
               return ChainStrategyAwardVO.builder()
                       .logicModel(ruleModel())
                       .awardId(awardId)
                       .awardRuleValue("0,10")
                       .build();
            }
        }
        // 过滤其他责任链
        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        return next().logic(userId, strategyId);
    }

    @Override
    protected String ruleModel() {
        return LogicModel.RULE_BLACKLIST.getCode();
    }
}
